<template>
	<view class="content">
		<view class="header">
			<image src="https://hotelfiles.oss-cn-zhangjiakou.aliyuncs.com/miniapp/static/choujiangpage.png?x-oss-process=image/resize,m_lfit,w_375" mode=""></image>
			<LotteryDraw :grid_info_arr='gridInfoArr'
			 class='LotteryDraw' 
			 :class='{heightPhone:isHeight}'
			 @get_winingIndex='get_winingIndex' 
			 @luck_draw_finish='luck_draw_finish'></LotteryDraw>
		</view>
	</view>
</template>

<script>
	import LotteryDraw from '../../../components/SJ-LotteryDraw/SJ-LotteryDraw.vue';
	import {lotteryChance,lotteryDraw} from '@/api/api.js'
	export default {
		components: {
			LotteryDraw
		},
		data() {
			return {
				lottery_draw_param: {
					startIndex: 0, //开始抽奖位置，从0开始
					totalCount: 3, //一共要转的圈数
					winingIndex: 4, //中奖的位置，从0开始
					speed: 50 //抽奖动画的速度 [数字越大越慢,默认100]
				},
				isHeight:false,
				gridInfoArr: [],
				chane:{},
				isDraw:true
			};
		},
		onLoad() {
			uni.getSystemInfo({
				success: (data) => {
					if(data.screenWidth/data.screenHeight>0.5){
						this.isHeight = true;
					}
				}
			})
			this.getLotteryChance()
		},
		methods: {
			getLotteryChance(){
				lotteryChance().then(res=>{
					if(res.code==200){
						this.chane = res.data;
						this.gridInfoArr = res.data.prizes;
						this.gridInfoArr.push({});
					}
				})
			},
			// 修改获奖位置（可以在这里获取后台的数据
			async get_winingIndex(callback) {
				try{
					if(this.isDraw){
						this.isDraw = false;
						let res = await lotteryDraw({id:this.chane.id});
						if(res.code!=200){
							this.isDraw = true;
							return;
						}
						if(!res.data){
							this.isDraw = true;
							uni.showToast({
								title:"暂无抽奖次数",
								icon:"none"
							})
							return;
						}
						for(let i=0;i<this.gridInfoArr.length;i++){
							if(this.gridInfoArr[i].id == res.data.id){
								this.lottery_draw_param.winingIndex  = i;
								break;
							}
						}
						callback(this.lottery_draw_param);
					}else{
						return;
					}
				}catch(e){
					//TODO handle the exception
					console.log(e);
				}
				
				//props修改在小程序和APP端不成功，所以在这里使用回调函数传参，
			},
			// 抽奖完成
			luck_draw_finish(param) {
				this.isDraw = true;
				uni.showToast({
					icon:"none",
					title:"恭喜你抽到"+param.name+",请三十天内领取"
				})
				console.log(param)
				// console.log(`抽到第${param+1}个方格的奖品`)
			}

		}
	};
</script>

<style lang="scss" scoped>
	.content {
		width: 100vw;
		height: 100vh;
		.header{
			height: 100%;
			width: 100%;
			position: relative;
			&>image{
				position: absolute;
				width: 100vw;
				height: 100vh;
			}
			.LotteryDraw {
				position: absolute;
				transform: scale(1.1);
				top: 49%;
				left: 110rpx;
			}
			.heightPhone.LotteryDraw{
				top: 43.5%;
			}
		}
	}
</style>
